package logic

import (
	"context"
	"errors"
	"novel-sys/model/mysql"

	"novel-sys/goods/goods"
	"novel-sys/goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BooksAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBooksAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BooksAddLogic {
	return &BooksAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BooksAddLogic) BooksAdd(in *goods.BooksAddRequest) (*goods.BooksAddResponse, error) {
	// todo: add your logic here and delete this line
	data := mysql.Books{
		UserId:  in.UserId,
		NovelId: in.NovelId,
	}
	var book mysql.Books

	if err := book.BooksById(mysql.DB, in); err != nil {
		return nil, err
	}
	if book.ID != 0 {
		return nil, errors.New("不能重复添加")
	}
	if err := data.BooksAdd(mysql.DB); err != nil {
		return nil, err
	}
	return &goods.BooksAddResponse{
		BooksId: int64(data.ID),
	}, nil
}
